package com.yhp.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

//解决跨域问题
@Configuration
public class CorsConfig {

    public CorsConfig(){}

    @Bean
    public CorsFilter corsFilter(){
        //1.设置cors配置信息
        CorsConfiguration configuration = new CorsConfiguration();
        //configuration.addAllowedOrigin("http://192.168.36.128:8080");
        configuration.addAllowedOrigin("http://localhost:8080");
        //设置是否可以发送cookie信息
        configuration.setAllowCredentials(true);
        //设置允许的请求方式，如get,post
        configuration.addAllowedMethod("*");
        //设置允许的header
        configuration.addAllowedHeader("*");
        //2.为url添加映射路径
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",configuration);
        //3.返回重新定义好的CorsSource
        return  new CorsFilter(urlBasedCorsConfigurationSource);
    }

}
